package org.darcy.controller.admin.article;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.darcy.entity.BizType;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.ResponseStatus;
import org.darcy.framework.model.PageResult;
import org.darcy.framework.model.ResponseVO;
import org.darcy.framework.util.ResultUtil;
import org.darcy.model.BizTypeModel;
import org.darcy.service.BizTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 文章类型管理
 *
 */
@RestController
@RequestMapping("/type")
public class TypeController {
	
	@Autowired
	private BizTypeService typeService;

	@RequiresPermissions("types")
	@PostMapping("/list")
	public PageResult list(BizTypeModel param) {
		Page p = new Page(param.getP(), param.getPageSize());
		IPage pageInfo = typeService.getPage(p, null);
		return ResultUtil.tablePage(pageInfo);
	}

	@RequiresPermissions("type:add")
	@PostMapping(value = "/add")
	@BussinessLog("添加分类")
	public ResponseVO add(BizType type) {
		typeService.insert(type);
		return ResultUtil.success("文章类型添加成功！新类型 - " + type.getName());
	}

	@RequiresPermissions(value = { "type:batchDelete", "type:delete" }, logical = Logical.OR)
	@PostMapping(value = "/remove")
	@BussinessLog("删除分类")
	public ResponseVO remove(Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		for (Long id : ids) {
			typeService.delete(id);
		}
		return ResultUtil.success("成功删除 [" + ids.length + "] 个文章类型");
	}

	@RequiresPermissions("type:get")
	@PostMapping("/get/{id}")
	@BussinessLog("获取分类详情")
	public ResponseVO get(@PathVariable Long id) {
		return ResultUtil.success(null, this.typeService.getById(id));
	}

	@RequiresPermissions("type:edit")
	@PostMapping("/edit")
	@BussinessLog("编辑分类")
	public ResponseVO edit(BizType type) {
		try {
			typeService.update(type);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("文章类型修改失败！");
		}
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

	@PostMapping("/listAll")
	public ResponseVO listType() {
		return ResultUtil.success(null, typeService.getList(null));
	}

	@PostMapping("/listParent")
	public ResponseVO listParent() {
		return ResultUtil.success(null, typeService.listParent());
	}

}
